package com.shh.poi;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.joda.time.DateTime;
import org.junit.Test;
import java.io.FileOutputStream;
import java.io.IOException;

public class ExcelWriteTest {
    @Test
    public void test1() throws IOException {

        Workbook workbook = new XSSFWorkbook();
        final Sheet sheet = workbook.createSheet("矿山争霸");
        final Row row = sheet.createRow(0);
        final Cell cell = row.createCell(0);
        cell.setCellValue("helloworld");
        final Cell cell1 = row.createCell(1);
        cell1.setCellValue("fuckyou!");
        final Row row1 = sheet.createRow(1);
        final Cell cell2 = row1.createCell(0);
        final String time = new DateTime().toString("yyyy-MM-dd HH:mm:ss");
        cell2.setCellValue(time);
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream
                    = new FileOutputStream("src/main/resources/poi.xlsx");
            workbook.write(fileOutputStream);
        } finally {
            if (fileOutputStream != null) {
                fileOutputStream.close();
            }
        }
    }


    @Test
    public void test2() throws IOException {
        final long begin = System.currentTimeMillis();
        Workbook workbook = new HSSFWorkbook();
        final Sheet sheet = workbook.createSheet("矿山争霸");
        for (int rowNum = 0; rowNum <= 65536; rowNum++) {
            final Row row = sheet.createRow(rowNum);
            for (int cellNum = 0; cellNum < 10; cellNum++) {
                final Cell cell = row.createCell(cellNum);
                cell.setCellValue(cellNum);
            }
        }
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream
                    = new FileOutputStream("src/main/resources/poi_2.xls");
            workbook.write(fileOutputStream);
        } finally {
            if (fileOutputStream != null) {
                fileOutputStream.close();
            }
        }
        System.out.println("file output over");
        final long end = System.currentTimeMillis();
        System.out.println(end-begin);
    }


    /**
     * XSSF 200000 行 10839ms
     * @throws IOException
     */
    @Test
    public void test3() throws IOException {
        final long begin = System.currentTimeMillis();
        Workbook workbook = new XSSFWorkbook();
        final Sheet sheet = workbook.createSheet("矿山争霸2");
        for (int rowNum = 0; rowNum <= 200000; rowNum++) {
            final Row row = sheet.createRow(rowNum);
            for (int cellNum = 0; cellNum < 10; cellNum++) {
                final Cell cell = row.createCell(cellNum);
                cell.setCellValue(cellNum);
            }
        }
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream
                    = new FileOutputStream("src/main/resources/poi_2.xlsx");
            workbook.write(fileOutputStream);
        } finally {
            if (fileOutputStream != null) {
                fileOutputStream.close();
            }
        }
        System.out.println("file output over");
        final long end = System.currentTimeMillis();
        System.out.println(end-begin);
    }


    /**
     * SXSSF 200000 行 1956ms
     * @throws IOException
     */
    @Test
    public void test4() throws IOException {
        final long begin = System.currentTimeMillis();
        Workbook workbook = new SXSSFWorkbook();
        final Sheet sheet = workbook.createSheet("矿山争霸2");
        for (int rowNum = 0; rowNum <= 200000; rowNum++) {
            final Row row = sheet.createRow(rowNum);
            for (int cellNum = 0; cellNum < 10; cellNum++) {
                final Cell cell = row.createCell(cellNum);
                cell.setCellValue(cellNum);
            }
        }

        try(FileOutputStream fileOutputStream
                    = new FileOutputStream("src/main/resources/poi_3.xlsx")){
            workbook.write(fileOutputStream);
            // 清楚SXSSF临时文件加速机制带来的临时文件
            ((SXSSFWorkbook)workbook).dispose();
        }
        System.out.println("file output over");
        final long end = System.currentTimeMillis();
        System.out.println(end-begin);
    }
}
